﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

///<summary>
///
///</summary>
public class BalloonSubject : MonoBehaviour
{
	List<BalloonInputKeyComponent> balloonList = new List<BalloonInputKeyComponent>();
	BalloonInputKeyComponent[] balloonInputs;
	GameObject balloon;
	GameObject[] balloons;
	BalloonType curType;
	BalloonType[] balloonTypes;

	Vector3[] rotate = { new Vector3(0,0),new Vector3(0,0,20),new Vector3(0,0,-20),new Vector3(0,0,15),new Vector3(0,0,-15)};
	Vector3[] newPos = { new Vector3(12, 227), new Vector3(-125, 211), new Vector3(107,182 ), new Vector3(-109, 316), new Vector3(109, 311) };
	// Start is called before the first frame update
    public BalloonType GetCurType()
    {
        return curType;
    }
	void OnEnable()
	{
		curType = RandomGetBalloonType();
		if (!this.name.Equals("Enemy1(Clone)") && !this.name.Equals("Enemy3(Clone)"))
			BalloonActiveOrInstance2();
		else if (!this.name.Equals("Enemy1(Clone)") && !this.name.Equals("Enemy2(Clone)"))
			BalloonActiveOrInstance3();
		else
			BalloonActiveOrInstance();

		balloonInputs = GetComponentsInChildren<BalloonInputKeyComponent>();
		for(int i = 0; i < balloonInputs.Length; i++)
		{
			balloonList.Add(balloonInputs[i]);
		}
	}
	public void BalloonBreak()
	{
		for (int i = 0; i < balloonInputs.Length; i++)
		{
			balloonList[i].LightningBreak();
		}
	}
	
	void BalloonActiveOrInstance3()
	{
		int num = UnityEngine.Random.Range(2, 4);
		balloons = new GameObject[num];
		balloonTypes = new BalloonType[num];
		for (int i = 0; i < num; i++)
		{
			balloonTypes[i] = RandomGetBalloonType();
			balloons[i] = BalloonFactory.Instance.GetGameObject("Balloon/" + balloonTypes[i].ToString(),
			gameObject, new Vector3(transform.position.x + newPos[i].x, transform.position.y + newPos[i].y), balloonTypes[i]);
			balloons[i].transform.eulerAngles = rotate[i];
			balloons[i].transform.SetSiblingIndex(0);
		}

	}

	void BalloonActiveOrInstance2()
	{
		int num = UnityEngine.Random.Range(3, 6);
		balloons = new GameObject[num];
		balloonTypes = new BalloonType[num];
		for (int i = 0; i < num; i++)
		{
			balloonTypes[i] = RandomGetBalloonType();
			balloons[i] = BalloonFactory.Instance.GetGameObject("Balloon/" + balloonTypes[i].ToString(),
			gameObject, new Vector3(transform.position.x+newPos[i].x,transform.position.y+newPos[i].y), balloonTypes[i]);
			balloons[i].transform.eulerAngles = rotate[i];
			balloons[i].transform.SetSiblingIndex(0);
		}
		
	}

	void BalloonActiveOrInstance()
	{
		if(balloon == null|| balloon.name != curType.ToString())
		{
			InstanceBalloon();
		}
		else
		{
			balloon.SetActive(true);
            balloon.transform.eulerAngles = new Vector3(0, 0, 0);
			balloon.transform.SetParent(transform);
		}
        SetLayer(0);
    }
    void InstanceBalloon()
	{
		balloon = BalloonFactory.Instance.GetGameObject("Balloon/" + curType.ToString(),
			gameObject, new Vector3(transform.position.x, transform.position.y + 200), curType);
	}

	void SetLayer(int num)
	{
		balloon.transform.SetSiblingIndex(num);
	}


	// Update is called once per frame
	void Update()
    {
        if (!GameConfig.IsGameOver)
            return;
        if (Camera.main.ScreenToWorldPoint(transform.position).y > 5.3f)
			return;
		if (Input.anyKeyDown)
		{
			foreach (KeyCode keyCode in Enum.GetValues(typeof(KeyCode)))
			{
				if (Input.GetKeyDown(keyCode))
				{
					foreach (BalloonInputKeyComponent inptuKey in balloonList)
					{
						inptuKey.CheckKey(keyCode);
					}
				}
			}
		}
    }
	bool inspectList(BalloonType balloonType)
	{
		if (GameConfig.typeList.Count >= 11)
			GameConfig.typeList.Clear();
		foreach (BalloonType type in GameConfig.typeList)
		{
			if (balloonType == type || balloonType.ToString().Equals(type.ToString()))
			{
				return false;
			}
		}
		GameConfig.typeList.Add(balloonType);
		return true;
	}
	BalloonType RandomGetBalloonType()
	{
		BalloonType cur = (BalloonType)UnityEngine.Random.Range(0, 11);
		while (true)
		{
			if (inspectList(cur))
			{
				break;
			}
			else
			{
				cur = (BalloonType)UnityEngine.Random.Range(0, 11);
			}
		}
		return cur;
	}
}
